<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Show extends \Phpcmf\Home\Module
{

    public function index() {
        $this->_module_init();
        $this->_Show(
            (int)\Phpcmf\Service::L('Input')->get('id'),
            [
                'field' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('field')),
                'value' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('value')),
            ],
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    // 关注操作
    public function guanzhu() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        if (!$this->uid) {
            $this->_json(0, dr_lang('你没有登录，无法进行关注操作'));
        }

        $this->_module_init();

        $zone = $this->content_model->table($this->content_model->mytable)->get($id);
        if (!$zone) {
            $this->_json(0, dr_lang('对方没有开通空间，无法进行关注操作'));
        }

        $my = $this->content_model->table($this->content_model->mytable)->where('uid', $this->uid)->getRow();
        if (!$my) {
            $this->_json(0, dr_lang('你没有开通空间，无法进行关注操作'));
        }

        // 判断是否是对方的粉丝
        if ($fensi = $this->content_model->table($this->content_model->mytable.'_form_fensi')->where('cid', $id)->where('uid', $this->uid)->getRow()) {
            // 是对方粉丝时，就表示关注了，现在就取消关注
            $this->content_model->table($this->content_model->mytable.'_form_fensi')->delete($fensi['id']); // 删除对方粉丝表
            $this->content_model->db->table($this->content_model->mytable.'_form_guanzhu')->where('cid', $my['id'])->where('uid', $zone['uid'])->delete(); // 删除自己的关注表

            // 更新统计
            $total = $this->content_model->table($this->content_model->mytable.'_form_fensi')->where('status', 1)->where('cid', $id)->counts();
            $this->content_model->table($this->content_model->mytable)->update($id, [
                'fensi_total' => $total,
            ]);
            $total = $this->content_model->table($this->content_model->mytable.'_form_guanzhu')->where('status', 1)->where('cid', $my['id'])->counts();
            $this->content_model->table($this->content_model->mytable)->update($my['id'], [
                'guanzhu_total' => $total,
            ]);

            $this->_json(1, dr_lang('已取消关注'), ['value' => 0]);
        } else {
            // 开始关注他
            // 加入对方粉丝表
            $this->content_model->table($this->content_model->mytable.'_form_fensi')->insert([
                'cid' => $id,
                'catid' => 0,
                'uid' => $this->uid,
                'author' => $this->member['username'],
                'title' => '',
                'status' => 1,
                'tableid' => 0,
                'displayorder' => 0,
                'inputip' => \Phpcmf\Service::L('input')->ip_address(),
                'inputtime' => SYS_TIME,
            ]);
            // 再把对方加入到我的关注表
            $this->content_model->table($this->content_model->mytable.'_form_guanzhu')->insert([
                'cid' => $my['id'],
                'catid' => 0,
                'uid' => $zone['uid'],
                'author' => $zone['author'],
                'title' => '',
                'status' => 1,
                'tableid' => 0,
                'displayorder' => 0,
                'inputip' => \Phpcmf\Service::L('input')->ip_address(),
                'inputtime' => SYS_TIME,
            ]); // 加入对方粉丝表

            // 更新统计
            $total = $this->content_model->table($this->content_model->mytable.'_form_fensi')->where('status', 1)->where('cid', $id)->counts();
            $this->content_model->table($this->content_model->mytable)->update($id, [
                'fensi_total' => $total,
            ]);
            $total = $this->content_model->table($this->content_model->mytable.'_form_guanzhu')->where('status', 1)->where('cid', $my['id'])->counts();
            $this->content_model->table($this->content_model->mytable)->update($my['id'], [
                'guanzhu_total' => $total,
            ]);

            $this->_json(1, dr_lang('已关注'), ['value' => 1]);
        }


    }

    public function time() {
        $this->_module_init();
        $this->_MyShow(
            'time',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function recycle() {
        $this->_module_init();
        $this->_MyShow(
            'recycle',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function draft() {
        $this->_module_init();
        $this->_MyShow(
            'draft',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function verify() {
        $this->_module_init();
        $this->_MyShow(
            'verify',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

}
